// import {CodeToText, TextToCode} from 'element-china-area-data'

export default {
  getStorage(key) {
    try {
      return JSON.parse(localStorage.getItem(key))
    } catch (e) {
      return localStorage.getItem(key)
    }
  },
  putStorage(key, value) {
    return localStorage.setItem(key, typeof value == 'object' ? JSON.stringify(value) : value)
  },
  removeStorage(key) {
    localStorage.removeItem(key)
  },
  clearStorage() {
    localStorage.clear()
  },
  getSession(key) {
    try {
      return JSON.parse(sessionStorage.getItem(key))
    } catch (e) {
      return sessionStorage.getItem(key)
    }
  },
  putSession(key, value) {
    return sessionStorage.setItem(key, typeof value == 'object' ? JSON.stringify(value) : value)
  },
  removeSession(key) {
    sessionStorage.removeItem(key)
  },
  clearSession() {
    sessionStorage.clear()
  },
  toFixed(num, decimal) {
    const numStr = num + ''
    if (numStr.indexOf('.') > -1) return parseFloat(num).toFixed(decimal || 2)
    else return parseFloat(num)
  },
  toFixedDecimal(num, decimal) {
    return parseFloat(num).toFixed(decimal || 2)
  },
  getStyle(dom, attr) {
    return dom.currentStyle ? dom.currentStyle[attr] : getComputedStyle(dom, null)[attr]
  },
  dynamicPX(pixel) {
    return document.documentElement.clientWidth / 10 / 192 * pixel
  },
  px2rem(pixel) {
    return this.toFixed(pixel / 192, 10)
  },
  // regionC2T(code) {
  //   return CodeToText[code]
  // },
  // regionT2C(text, origin = TextToCode, result = []) {
  //   let keys = Object.keys(origin), key = keys.find(item => text.startsWith(item))
  //   if (key) {
  //     origin = origin[key]
  //     result.push(origin.code)
  //     return this.regionT2C(text.substr(key.length), origin, result)
  //   } else if (origin['市辖区']) {
  //     origin = origin['市辖区']
  //     result.push(origin.code)
  //     return this.regionT2C(text, origin, result)
  //   } else return result
  // },
  resize(fn) {
    if (fn && fn instanceof Function) {
      fn()
      window.addEventListener('resize', fn, false)
    }
  },
  scroll(fn) {
    if (fn && fn instanceof Function) {
      fn()
      window.addEventListener('scroll', fn, false)
    }
  },
  stringToByte(str) {
    let len, c;
    len = str.length
    let bytes = []
    for (let i = 0; i < len; i++) {
      c = str.charCodeAt(i)
      if (c >= 0x010000 && c <= 0x10FFFF) {
        bytes.push(((c >> 18) & 0x07) | 0xF0)
        bytes.push(((c >> 12) & 0x3F) | 0x80)
        bytes.push(((c >> 6) & 0x3F) | 0x80)
        bytes.push((c & 0x3F) | 0x80)
      } else if (c >= 0x000800 && c <= 0x00FFFF) {
        bytes.push(((c >> 12) & 0x0F) | 0xE0)
        bytes.push(((c >> 6) & 0x3F) | 0x80)
        bytes.push((c & 0x3F) | 0x80)
      } else if (c >= 0x000080 && c <= 0x0007FF) {
        bytes.push(((c >> 6) & 0x1F) | 0xC0)
        bytes.push((c & 0x3F) | 0x80)
      } else bytes.push(c & 0xFF)
    }
    return bytes
  },
  hidePhone(phone) {
    return phone && phone.length == 11 ? `${phone.substring(0, 3)}******${phone.substring(9)}` : phone;
  },
  startAnimation(callback) {
    const fn = requestAnimationFrame || oRequestAnimationFrame || msRequestAnimationFrame || mozRequestAnimationFrame || webkitRequestAnimationFrame || (function () {
      var lastTime = 0
      return function (callback) {
        var currTime = Date.now()
        var timeToCall = Math.max(0, 16 - (currTime - lastTime))
        var id = setTimeout(function () {
          callback(currTime + timeToCall)
        }, timeToCall)
        lastTime = currTime + timeToCall
        return id
      }
    })()
    return fn(callback)
  },
  stopAnimation(id) {
    const fn = cancelAnimationFrame || cancelRequestAnimationFrame || oCancelAnimationFrame || oCancelRequestAnimationFrame || msCancelAnimationFrame || msCancelRequestAnimationFrame || mozCancelAnimationFrame || mozCancelRequestAnimationFrame || webkitCancelAnimationFrame || webkitCancelRequestAnimationFrame || function (id) {
      clearTimeout(id)
    }
    fn(id)
  }
}
